package com.library.controller;

import com.library.entity.Admin;
import com.library.entity.User;
import com.library.exception.LoginErrorException;
import com.library.exception.UserRepeatException;
import com.library.service.IAdminService;
import com.library.service.IUserService;
import com.library.util.AuthImageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;

/**
 * 登录注册
 * Created by linzhi on 2016/12/12.
 */
@Controller
@RequestMapping("/login")
public class LoginController {
    @Resource
    private IUserService userService;
    @Resource
    private IAdminService adminService;

    /**
     * 跳转到注册页面
     */
    @RequestMapping("/registerUI")
    public String registerUI() {
        return "login/register";
    }

    /**
     * 注册
     * @param user 用户名和密码
     * @param code 验证码
     * @param confirm 确认密码
     */
    @RequestMapping("/register")
    public String register(User user, String code, String confirm, HttpServletRequest request) {
        if(!code.toLowerCase().equals(request.getSession().getAttribute(AuthImageUtil.CODE))) { //判断验证码是否正确
            request.setAttribute("msg","验证码不正确!");
        } else if (!user.getPassword().equals(confirm)) {
            request.setAttribute("msg","密码和确认密码不一致!");
        } else {
            //注册
            user.setCreateTime(new Timestamp(new Date().getTime()));
            try {
                userService.register(user);
            } catch (UserRepeatException e) {//用户名重复
                request.setAttribute("msg", e.getMessage());
                return "login/register";
            }

            //重定向到登录页面
            return "redirect:/login/loginUI.do";
        }
        return "login/register";
    }

    /**
     * 跳转到登录页面
     */
    @RequestMapping("/loginUI")
    public String loginUI() {
        return "login/login";
    }

    /**
     * 登录
     * @param user 用户名、密码
     * @param code 验证码
     */
    @RequestMapping("/login")
    public String login(User user, String code, HttpServletRequest request) {
        if(!code.toLowerCase().equals(request.getSession().getAttribute(AuthImageUtil.CODE))) { //判断验证码是否正确
            request.setAttribute("msg","验证码不正确!");
        } else {
            //登录
            try {
                User loginUser = userService.login(user);

                //把用户信息设置到session中
                request.getSession().setAttribute("loginUser",loginUser);

                return "redirect:/home/indexUI.do";
            } catch (LoginErrorException e) {
                request.setAttribute("msg",e.getMessage());
            }
        }
        return "login/login";
    }

    /**
     * 注销
     */
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request) {
        request.getSession().removeAttribute("loginUser");
        return "redirect:/login/loginUI.do";
    }

    /**
     * 获取验证码
     */
    @RequestMapping("/getCode")
    public void getImage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        AuthImageUtil.getCode(response, request, AuthImageUtil.CODE);
    }

    /**
     * 跳转到管理员登录页面
     */
    @RequestMapping("/adminLoginUI")
    public String adminLoginUI() {
        return "login/adminLogin";
    }

    /**
     * 管理员登录
     * @param admin 用户名、密码
     * @param code 验证码
     */
    @RequestMapping("/adminLogin")
    public String adminLogin(Admin admin, String code, HttpServletRequest request) {
        if(!code.toLowerCase().equals(request.getSession().getAttribute(AuthImageUtil.CODE))) { //判断验证码是否正确
            request.setAttribute("msg","验证码不正确!");
        } else {
            //登录
            try {

                Admin loginAdmin = adminService.login(admin);

                //把用户信息设置到session中
                request.getSession().setAttribute("loginAdmin",loginAdmin);

                return "redirect:/admin/indexUI.do";
            } catch (LoginErrorException e) {
                request.setAttribute("msg",e.getMessage());
            }
        }
        return "login/adminLogin";
    }

    /**
     * 管理员注销
     */
    @RequestMapping("/adminLogout")
    public String adminLogout(HttpServletRequest request) {
        request.getSession().removeAttribute("loginAdmin");
        return "redirect:/login/adminLoginUI.do";
    }
}
